<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CronJOb extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		//$this->load->model('games');
		$this->load->model('game'); 
	}
	
	// deifrerence in minutes
	function dateDiference($past,$present,$diference)
	{
		
		$to_time = strtotime($present);
		$from_time = strtotime($past);
				
		if( round(abs($to_time - $from_time) / 60,2)<$diference){
			
			
			return true;
		}
		
		
		//echo 'false';
	}
	
	public function index()
	{
		
		$this->load->library('prosoccer_parser');
		$parser= new Prosoccer_parser();
		//$arrayOfGames = $parser->parseMainPage('http://www.prosoccer.gr');	
		//http://www.prosoccer.gr/soccer-prediction-Tuce.htm
		//http://www.prosoccer.gr/soccer-prediction-Wece.htm
		//http://www.prosoccer.gr/soccer-prediction-Thce.htm
		//http://www.prosoccer.gr/soccer-prediction-Frce.htm
		//http://www.prosoccer.gr/soccer-prediction-Sace.htm
		//http://www.prosoccer.gr/soccer-prediction-Suce.htm
		//http://www.prosoccer.gr/soccer-prediction-Moce.htm	
		$parseTimeStart=time();
		$arrayOfGames = $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Tuce.htm');		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Wece.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Thce.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Frce.htm'));
		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Sace.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Suce.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Moce.htm'));
		$parseTime=time()-$parseTimeStart;
		
		// load all games from db
		$dbGames = $this->game->getAllGames();
		$contain = false;
		$insert=0;
		$update=0;
		//echo '<pre>';
		//print_r($arrayOfGames);
		//echo '</pre>';
		$loopTimeStart=time();
		foreach ($arrayOfGames as $gameCron){
			//echo '<pre>';
			//echo $gameCron->toString();
			//echo '</pre>';
			$contain =false;
			foreach ($dbGames as $gameDb){	
				
				// ako posti u zadnjih 10 dana 	i home team i away team i  league i rezultat " - " onda --> update kvote 1, x, 2 , rezultat
				if ($this->dateDiference($gameDb->getTimestamp(),$gameCron->getTimestamp(),14400) && $gameDb->getHomeTeam()==$gameCron->getHomeTeam() && $gameDb->getAwayTeam()==$gameCron->getAwayTeam() && $gameDb->getLeague()==$gameCron->getLeague())
				{
					if ((($gameDb->getResult()==' - ') && $gameCron->getResult()!=' - ') || ($gameDb->getPercentFix1()=='') || ($gameDb->getPercentFixX()=='') || ($gameDb->getPercentFix2())=='')
					{
											
						$this->game->updateGame($gameDb,$gameCron);
						$update++;
						
					}
					$contain =true;
					break;
				}			
				
			}
			
			if (!($contain)){
				$this->game->insertGame($gameCron);
				$insert++;
				$contain =false;
			}
			
			
			
		}
		$loopTime=time()-$loopTimeStart;
		$data = array(
				'parseTime' => $parseTime,
				'loopTime' => $loopTime,
				'insert' => $insert,
				'update' => $update,				
		);		
		
		
		//$this->games->insertAllGames($arrayOfGames);		
		$this->load->view('cronJob',$data);
	}
	
}

/*
 * 1. pokupimo sve utakmice
 * 2. idem od jedne do druge utakmice 
 * 3. 
 * 
 * 
 * 
 * 
 */





?>

